function ajax(obj) {
    // 结构
    // 默认发起get请求
    let { url, type = "get", data, success, failed } = obj;
    // console.log(url);
    // console.log(type);
    // console.log(data);
    // console.log(success); //函数 -- 请求成功以后调用

    let arr = [];
    for (let key in data) {
        // console.log(key); //username
        let value = data[key];
        // console.log(value); //zhangsan
        let str = key + "=" + value;
        // console.log(str);
        arr.push(str);
    }
    // console.log(arr);
    // console.log(arr.join("&")); //username=zhangsan&password=12345

    // data   {username:'zhangsan',password:'123'}
    // username=zhangsan&password=123 -->  url? username=zhangsan&password=123

    if (type == "get") {
        url = url + "?" + arr.join("&");
        // console.log(url);
        let xhr = new XMLHttpRequest();
        xhr.open(type, url);
        xhr.send();
        xhr.onload = function () {
            if (xhr.status == 200) {
                let obj = JSON.parse(xhr.responseText);
                // 请求成功了
                success(obj);
            }
        };
        xhr.onerror = function () {
            // console.log("请求网络出错 会执行");
            failed();
        };
    } else if (type == "post") {
        // console.log(url);
        let xhr = new XMLHttpRequest();
        xhr.open(type, url);
        xhr.setRequestHeader(
            "content-type",
            "application/x-www-form-urlencoded"
        );
        xhr.send(arr.join("&"));
        xhr.onload = function () {
            if (xhr.status == 200) {
                let obj = JSON.parse(xhr.responseText);
                // 请求成功了
                success(obj);
            }
        };

        xhr.onerror = function () {
            // console.log("请求网络出错 会执行");
            failed();
        };
    }
}